# controller

# responsible for the build of the acl-information-screen
# puts the html-pieces together

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from Utilities import * 

class BuildAclInfo(webapp.RequestHandler):
    def get(self, pSelf, key, md5, acl, templateValues, permissions):
        utilities = Utilities()
        
#        utilities.doTranslation()
        
        # menu-text
        credentialsText = utilities.getMenuTextCredentials()
        yourDataText = utilities.getMenuTextYourData()
        infoText = utilities.getMenuTextInfo()
        
        # get data for some place-holder
        title = utilities.getTitleText()
        userText = templateValues['userText']
        username = templateValues['user']
        url = templateValues['url']
        urlText = templateValues['urlText']
        hintText = templateValues['hintText']
        hintColor = templateValues['hintColor']
        
        # data for some other place-holder
        headline = self.getAclMgmtText()
        entryInfoTableHeadline = self.getInfoTableText()
        entryInfoTable = self.buildEntryInfoTable(key, md5, acl)
        aclTableHeadline = self.getAclTableText()
        aclTable = self.buildAclTable(permissions)
        changeAclHeadline = self.getChangeAclText()
        changeAcl = self.buildChangeAcl(key, md5)
        
        finalTemplateValues = {
                               'title': title,
                               'userText': userText,
                               'user': username,
                               'url': url,
                               'urlText': urlText,
                               'hintText': hintText,
                               'hintColor': hintColor,
                               'credentials': credentialsText,
                               'yourData': yourDataText,
                               'info': infoText,
                               'headline': headline,
                               'entryInfoTableHeadline': entryInfoTableHeadline,
                               'entryInfoTable': entryInfoTable,
                               'aclTableHeadline': aclTableHeadline,
                               'aclTable': aclTable,
                               'changeAclHeadline': changeAclHeadline,
                               'changeAcl': changeAcl
                               }
        
        html = template.render('view/templates/acl.html', finalTemplateValues)
        
        pSelf.response.out.write(html)
        
    # contians the headline-text
    def getAclMgmtText(self):
#        headline = _('Access-Control-List Management')
        headline = 'Access-Control-List Management'
        return headline
    
    # contains the into-table-text
    def getInfoTableText(self):
#        entryInfoTableHeadline = _('Key-Info')
        entryInfoTableHeadline = 'Key-Info'
        return entryInfoTableHeadline
        
    # contains the acl-table-text
    def getAclTableText(self):
#        aclTableHeadline = _('ACL-Settings')
        aclTableHeadline = 'ACL-Settings'
        return aclTableHeadline
    
    # contains the change-acl-text
    def getChangeAclText(self):
#        changeAclHeadline = _('Change ACL-Settings')
        changeAclHeadline = 'Change ACL-Settings'
        return changeAclHeadline
    
    # build up the key-info-table
    def buildEntryInfoTable(self, key, md5, acl):
        entryInfoTable = ''
        entryInfoTable = entryInfoTable + '<table border="1" cellspacing="0" cellpadding="5">'
        entryInfoTable = entryInfoTable + '<td>'
#        entryInfoTable = entryInfoTable + '<b>' + _('Keyname: ') + '</b>'
        entryInfoTable = entryInfoTable + '<b>' + 'Keyname: ' + '</b>'
        entryInfoTable = entryInfoTable + '<em>' + key + '</em>'
        entryInfoTable = entryInfoTable + '</td>'
        entryInfoTable = entryInfoTable + '<td>'
#        entryInfoTable = entryInfoTable + '<b>' + _('MD5-Checksum: ') + '</b>'
        entryInfoTable = entryInfoTable + '<b>' + 'MD5-Checksum: ' + '</b>'
        entryInfoTable = entryInfoTable + '<em>' + md5 + '</em>'
        entryInfoTable = entryInfoTable + '</td>'
        entryInfoTable = entryInfoTable + '<td>'
#        entryInfoTable = entryInfoTable + '<b>' + _('ACL: ') + '</b>'
        entryInfoTable = entryInfoTable + '<b>' + 'ACL: ' + '</b>'
        entryInfoTable = entryInfoTable + '<em>' + acl + '</em>'
        entryInfoTable = entryInfoTable + '</td>'
        entryInfoTable = entryInfoTable + '</table>'
        
        return entryInfoTable
    
    # build up the acl-table
    def buildAclTable(self, permissions):
        # private attributes - responsible for the tick.png or false.png
        # if the names of the files changed, it must only be correct here...
#        __permissionTrue = 'tick.png'
#        __permissionFalse = 'false.png'
        __permissionTrue = 'bullet_accept.png'
        __permissionFalse = 'bullet_deny.png'
        
        # get the acl/permissions
        AllUsersREAD  = permissions['AllUsersREAD']
        AllUsersWRITE = permissions['AllUsersWRITE']
        AllUsersFULL  = permissions['AllUsersFULL']
        AuthentUsersREAD   = permissions['AuthentUsersREAD']
        AuthentUsersWRITE  = permissions['AuthentUsersWRITE']
        AuthentUsersFULL   = permissions['AuthentUsersFULL']
        OwnerName   = permissions['OwnerName']
        OwnerREAD   = permissions['OwnerREAD']
        OwnerWRITE  = permissions['OwnerWRITE']
        OwnerFULL   = permissions['OwnerFULL']
        
        # build the acl-table
        aclTable = ''
        aclTable = aclTable + '<table border="1" cellspacing="0" cellpadding="5">'
        aclTable = aclTable + '<tr>'
        aclTable = aclTable + '<th align="center">'
#        aclTable = aclTable + _('User')
        aclTable = aclTable + 'User'
        aclTable = aclTable + '</th>'
        aclTable = aclTable + '<th align="center">'
#        aclTable = aclTable + _('Read')
        aclTable = aclTable + 'Read'
        aclTable = aclTable + '</th>'
        aclTable = aclTable + '<th align="center">'
#        aclTable = aclTable + _('Write')
        aclTable = aclTable + 'Write'
        aclTable = aclTable + '</th>'
        aclTable = aclTable + '<th align="center">'
#        aclTable = aclTable + _('Full Control')
        aclTable = aclTable + 'Full Control'
        aclTable = aclTable + '</th>'
        aclTable = aclTable + '</tr>'
        
        # selection for 'AllUsers...' - depend on what value assigned (true | false) the image-file is loaded
        aclTable = aclTable + '<tr>'
        aclTable = aclTable + '<td>'
#        aclTable = aclTable + '<b>' + _('Everyone') + '</b>'
        aclTable = aclTable + '<b>' + 'Everyone' + '</b>'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AllUsersREAD == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AllUsersREAD + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AllUsersREAD + '">'
        elif AllUsersREAD == 'false':
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AllUsersREAD + '">'        
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AllUsersREAD + '">'        
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AllUsersWRITE == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AllUsersWRITE + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AllUsersWRITE + '">'
        elif AllUsersWRITE == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AllUsersWRITE + '">'         
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AllUsersWRITE + '">'         
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AllUsersFULL == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AllUsersFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AllUsersFULL + '">'
        elif AllUsersFULL == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AllUsersFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AllUsersFULL + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '</tr>'
        
        # selection for 'AuthentUsers...' - depend on what value assigned (true | false) the image-file is loaded
        aclTable = aclTable + '<tr>'
        aclTable = aclTable + '<td>'
#        aclTable = aclTable + '<b>' + _('Authent. User') + '</b>'
        aclTable = aclTable + '<b>' + 'Authent. User' + '</b>'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AuthentUsersREAD == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AuthentUsersREAD + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AuthentUsersREAD + '">'
        elif AuthentUsersREAD == 'false':
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AuthentUsersREAD + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AuthentUsersREAD + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AuthentUsersWRITE == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AuthentUsersWRITE + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AuthentUsersWRITE + '">'
        elif AuthentUsersWRITE == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AuthentUsersWRITE + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AuthentUsersWRITE + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if AuthentUsersFULL == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ AuthentUsersFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ AuthentUsersFULL + '">'
        elif AuthentUsersFULL == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ AuthentUsersFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ AuthentUsersFULL + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '</tr>'
        
        # selection for 'Owner...' - depend on what value assigned (true | false) the image-file is loaded
        aclTable = aclTable + '<tr>'
        aclTable = aclTable + '<td>'
#        aclTable = aclTable + '<b>' + _('Owner') + '</b> (' + OwnerName + ')'
        aclTable = aclTable + '<b>' + 'Owner' + '</b> (' + OwnerName + ')'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if OwnerREAD == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ OwnerREAD + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ OwnerREAD + '">'
        elif OwnerREAD == 'false':
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ OwnerREAD + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ OwnerREAD + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if OwnerWRITE == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ OwnerWRITE + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ OwnerWRITE + '">'
        elif OwnerWRITE == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ OwnerWRITE + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ OwnerWRITE + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '<td align="center">'
        if OwnerFULL == 'true':
#            aclTable = aclTable + '<img src="view/images/' + __permissionTrue + '" width="20" height="20" border="0" alt="'+ OwnerFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionTrue + '" width="28" height="28" border="0" alt="'+ OwnerFULL + '">'
        elif OwnerFULL == 'false':            
#            aclTable = aclTable + '<img src="view/images/' + __permissionFalse + '" width="20" height="20" border="0" alt="'+ OwnerFULL + '">'
            aclTable = aclTable + '<img src="view/images/OctopusIconsScratch/01_Scratch/' + __permissionFalse + '" width="28" height="28" border="0" alt="'+ OwnerFULL + '">'
        aclTable = aclTable + '</td>'
        aclTable = aclTable + '</tr>'
        aclTable = aclTable + '<table>'
        
        return aclTable
        
    # build up the change-acl-fields
    def buildChangeAcl(self, key, md5):
        changeAcl = ''
        changeAcl = changeAcl + '<form action="/changeAcl?key=' + key + '&amp;md5=' + md5 + '" method="post" accept-charset="utf-8">'
        changeAcl = changeAcl + '<table>'
        changeAcl = changeAcl + '<td>'
        changeAcl = changeAcl + '<select name="acl" size="1">\n'
        changeAcl = changeAcl + '<option selected="selected">----------</option>\n'
        changeAcl = changeAcl + '<option>private</option>\n'
        changeAcl = changeAcl + '<option>authenticated-read</option>\n'
        changeAcl = changeAcl + '<option>public-read</option>\n'
        changeAcl = changeAcl + '<option>public-read-write</option>\n'
        changeAcl = changeAcl + '</select>\n'
        changeAcl = changeAcl + '</td>'
        changeAcl = changeAcl + '<td>'
        changeAcl = changeAcl + '<a href="/changeAcl" title="changeAcl">'
#        changeAcl = changeAcl + '<input type="submit" name="changeAcl" value="' + _('change') + '" >'
        changeAcl = changeAcl + '<input type="submit" name="changeAcl" value="' + 'change' + '" >'
        changeAcl = changeAcl + '</a>'
        changeAcl = changeAcl + '</form>'
        changeAcl = changeAcl + '</td>'
        changeAcl = changeAcl + '</table>'
        
        return changeAcl
        
        